<?php
class Zendvn_Paginator
{
	public function createPaginator($currentUrl, $totalItem, $currentPage, $itemCountPerPage = 10, $kw = false)
	{
		$kw = (false !== $kw) ? '?keyword=' . $kw : '';
		$totalPage = ceil($totalItem/$itemCountPerPage);
		if ($totalPage <= 1) return '';
		$v_f = 2;
		$v_a = 2;
		$v_l = 2;
		$max_pages = $v_f + $v_a + $v_l + 3;	
		$html = '';
		
		$pgt = $currentPage - 1;
		if ($currentPage != 1)
		{
			if ($totalPage > $max_pages)
			{
				$html .= '<a class="pagelink" href="'.$currentUrl.'/' .$kw. '" title ="Trang đầu"><b>Trang đầu</b></a>';
				//$html .= '<a class=pagelink href="'.$currentUrl . '.' . $pgt.'.html" onfocus=this.blur() title="' .$lang->line('prepage'). '"><b>&laquo;</b></a> ';
			}
		}
		for($m = 1; $m <= $totalPage; $m++) {
			if ($totalPage > $max_pages) {
				if (($m > $v_f) && (($m < $currentPage - $v_a) || ($m > $currentPage + $v_a)) && ($m < $totalPage - $v_l + 1)) {
					if ( ! isset($z_1) && ($m > $v_f)) {
						$html .= "...";
						$z_1 = true;
					}
					elseif ( ! isset($z_2) && ($m > $currentPage + $v_a)) {
						$html .= "...";
						$z_2 = true;
					}
					continue;
				}
			}	
			if($m == $currentPage)
				$html .= '<a class="pagecurrent">'.$m.'</a> ';
			else
				$html .= '<a href="'.$currentUrl . '-' . $m.'/' .$kw. '" title="Xem trang '.$m.'" class="pagelink">'.$m.'</a> ';
		}
		$pgs = $currentPage + 1;
		if ($currentPage != $totalPage) {
			if ($totalPage > $max_pages)
			{
				//$html .= '<a class=pagelink href="'.$currentUrl . '.' . $pgs.'.html" onfocus=this.blur() title="' .$lang->line('nextpage'). '"><b>&raquo;</b></a> ';	
				$html .= '<a class="pagelink" href="'.$currentUrl . '-' . $totalPage.'/' .$kw. '" title="Trang cuối"><b>Trang cuối</b></a>';
			}
		}
		$pageview = '
		<div id="paging">
			<div style="float: left;">
				<span class="page-text">Trang:</span> 
				' .$html. '
			</div>
			<div class="clr"></div>
		</div>';
		return $pageview;
	}
}